﻿namespace Gift.WebApp
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class AlipayService
    {
        private string _input_charset = "";
        private string _key = "";
        private string _sign_type = "";
        private string gateway = "";
        private string mysign = "";
        private Dictionary<string, string> sPara = new Dictionary<string, string>();

        public AlipayService(string partner, string seller_email, string return_url, string notify_url, string show_url, string out_trade_no, string subject, string body, string total_fee, string paymethod, string defaultbank, string anti_phishing_key, string exter_invoke_ip, string extra_common_param, string buyer_email, string royalty_type, string royalty_parameters, string key, string input_charset, string sign_type)
        {
            this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
            this._key = key.Trim();
            this._input_charset = input_charset.ToLower();
            this._sign_type = sign_type.ToUpper();
            SortedDictionary<string, string> dicArrayPre = new SortedDictionary<string, string>();
            dicArrayPre.Add("service", "create_direct_pay_by_user");
            dicArrayPre.Add("payment_type", "1");
            dicArrayPre.Add("partner", partner);
            dicArrayPre.Add("seller_email", seller_email);
            dicArrayPre.Add("return_url", return_url);
            dicArrayPre.Add("notify_url", notify_url);
            dicArrayPre.Add("_input_charset", this._input_charset);
            dicArrayPre.Add("show_url", show_url);
            dicArrayPre.Add("out_trade_no", out_trade_no);
            dicArrayPre.Add("subject", subject);
            dicArrayPre.Add("body", body);
            dicArrayPre.Add("total_fee", total_fee);
            dicArrayPre.Add("paymethod", paymethod);
            dicArrayPre.Add("defaultbank", defaultbank);
            dicArrayPre.Add("anti_phishing_key", anti_phishing_key);
            dicArrayPre.Add("exter_invoke_ip", exter_invoke_ip);
            dicArrayPre.Add("extra_common_param", extra_common_param);
            dicArrayPre.Add("buyer_email", buyer_email);
            dicArrayPre.Add("royalty_type", royalty_type);
            dicArrayPre.Add("royalty_parameters", royalty_parameters);
            this.sPara = AlipayFunction.Para_filter(dicArrayPre);
            this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
        }

        public string Build_Form()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + this.gateway + "_input_charset=" + this._input_charset + "\" method=\"get\">");
            foreach (KeyValuePair<string, string> pair in this.sPara)
            {
                builder.Append("<input type=\"hidden\" name=\"" + pair.Key + "\" value=\"" + pair.Value + "\"/>");
            }
            builder.Append("<input type=\"hidden\" name=\"sign\" value=\"" + this.mysign + "\"/>");
            builder.Append("<input type=\"hidden\" name=\"sign_type\" value=\"" + this._sign_type + "\"/>");
            builder.Append("<input type=\"submit\" value=\"支付宝确认付款\"></form>");
            builder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
            return builder.ToString();
        }
    }
}

